<template>
<div class="insert_background">
<el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
    prop="p_id"
    label="实践序号"
    width="180"
    >
    </el-table-column>
    <el-table-column
    prop="p_name"
    label="实践名称"
    width="200">
    </el-table-column>
    <el-table-column
    prop="address"
    label="实践地址"
    width="200">
    </el-table-column>
    <el-table-column
    prop="teacher.teacher_name"
    label="负责老师"
    width="200">
    </el-table-column>
    <el-table-column
    prop="start_time"
    label="起始时间"
    width="200">
    </el-table-column>
    <el-table-column
    prop="end_time"
    label="结束时间"
    width="200">
    </el-table-column>
    <el-table-column
      label="操作"
      width="95">
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.$index,scope.row,tableData)" type="text" size="small">添加</el-button>
      </template>
    </el-table-column>
  </el-table>
</div>
  
</template>

<script>
export default {
    data() {
      return {
        tableData: [],
        teacher1:[]
      }
    },
    created(){
        this.$axios.post('/api/student/findallpractice').then(res=>{
            this.tableData=res.data;
            res.data.forEach(element => {
              this.teacher1.push(element.teacher);
            });
             
        })
    },
    methods:{
        handleClick(index,row,rows) {
        let s_id = JSON.parse(sessionStorage.getItem("student")).s_id;
        this.$axios.post('/api/student/insertpractice',this.$qs.stringify({
        s_id:s_id,p_id:row.p_id,t_id:this.teacher1[index].t_id}))
        .then(
            ()=>{rows.splice(index, 1);
        this.$message.success('添加成功！')})
        .catch((error)=>
        console.log(error)
        )
      }
    }
}
</script>

<style>
</style>